package com.fengycx.autoconfigure;

import com.fengycx.properties.WechatPayProp;
import com.fengycx.templeate.WechatJsapiPayTemplate;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({WechatPayProp.class})
public class WechatJsapiPayAutoConfiguration {

    @Bean
    public WechatJsapiPayTemplate wechatJsapiPayTemplates(WechatPayProp wechatPayProp){
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wechatPayProp.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私s钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(wechatPayProp.getPrivateKeyPath())
                        .merchantSerialNumber(wechatPayProp.getMerchantSerialNumber())
                        .apiV3Key(wechatPayProp.getApiV3Key())
                        .build();

        // 初始化服务
        JsapiServiceExtension service =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();

        ProfitsharingService profitsharingService = new ProfitsharingService.Builder().config(config).build();

        return new WechatJsapiPayTemplate(wechatPayProp,config,service,profitsharingService);

    }


}
